<!--  -->
<template>
  <div class="login">
    <div class="Jump_div">
      <span class="Jump_span" @click="login">先看看，跳过 &gt;</span>
    </div>
    <div class="Come_use">
      <h2>欢迎进入电商App</h2>
    </div>

    <div class="input_item">
      <van-form @submit="onSubmit">
        <div class="input_title">手机号码</div>
        <van-field
          class="inpu—cell"
          v-model="phone"
          name="phone"
          label="+86"
          label-width="30px"
          placeholder="请输入手机号"
          :rules="[{ required: true, message: '手机号' }]"
        />
        <div class="input_title">密码</div>
        <van-field
          class="inpu—cell"
          v-model="password"
          type="password"
          name="password"
          label
          placeholder="请输入登录密码"
          :rules="[{ required: true, message: '密码' }]"
        />
        <div class="Submit_btn">
          <van-button block type="info" color="#9ebaea" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
    <div class="Fr_area">
      <span class="forget_pas">忘记密码</span>
      <span class="regist_user" @click="goRegister">立即注册</span>
    </div>
    <div class="tableTitle">
      <span class="midText">第三方登录</span>
    </div>
    <div class="third_party">
      <p v-for="(item, index) in sansList" :key="index">
        <svg-icon :iconClass="item.icon" class="third_icon"></svg-icon>
        <span>{{ item.text }}</span>
      </p>
    </div>
    <div class="copyright_footer">@最终版权归直播电商所有</div>
  </div>
</template>

<script>
import { Toast } from "vant";
import SvgIcon from "../../components/SvgIcon.vue";
import { mapMutations } from "vuex";
export default {
  //import引入的组件需要注入到对象中才能使用
  beforeRouteEnter(to, from, next) {
    if (window.sessionStorage.getItem("token")) {
      next("/layout/home");
    } else {
      next();
    }
    // 在渲染该组件的对应路由被 confirm 前调用
    // 不！能！获取组件实例 `this`
    // 因为当守卫执行前，组件实例还没被创建
  },
  components: { SvgIcon },
  data() {
    //这里存放数据
    return {
      phone: "",
      password: "",
      sansList: [
        {
          icon: "qq",
          text: "QQ",
        },
        {
          icon: "vx",
          text: "微信",
        },
        {
          icon: "vb",
          text: "微博",
        },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    ...mapMutations("login", ["SET_USERINFO"]),
    onSubmit(values) {
      // console.log("submit", values);
      this.$api.login(values).then((res) => {
        console.log(res, "res");
        if (res.status === 200) {
          this.$router.push("/layout/home");
          window.sessionStorage.setItem("token", res.data.token);
        } else {
          Toast.fail("账号密码错误");
        }
      });
    },
    goRegister() {
      this.$router.push("/register");
    },
    login() {
      this.$router.push("/layout/home");
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
};
</script>
<style lang="scss" scoped>
@import "@css/style.scss";
.login {
  width: 100%;
  height: 100%;
  .Jump_div {
    height: px2rem(40);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    position: relative;
    .Jump_span {
      font-size: $infoSize;
      color: #b0b0b0;
      margin-right: px2rem(15);
      font-weight: bold;
    }
  }
  .Come_use {
    width: 100%;
    height: px2rem(130);
    text-align: center;
    line-height: px2rem(130);
    h2 {
      font-size: px2rem(17);
      font-weight: bolder;
      text-align: center;
      margin-bottom: px2rem(36);
    }
  }
  .input_item {
    width: 100%;
    height: px2rem(200);
    padding: 0 px2rem(16);
    .input_title {
      font-size: px2rem(14);
      margin: 0 px2rem(15);
      margin-top: px2rem(5);
    }
    .Submit_btn {
      margin: 30px 14px 0 14px;
    }
  }
  .Fr_area {
    width: 100%;
    height: px2rem(30);
    margin-top: px2rem(15);
    padding: 0 px2rem(30);
    display: flex;
    align-items: center;
    justify-content: space-between;
    .forget_pas,
    .regist_user {
      font-size: px2rem(15);
      color: #ababab;
    }
    .regist_user {
      color: red;
    }
  }
  .tableTitle {
    position: relative;
    margin: px2rem(45) auto;
    width: 260px;
    height: 1px;
    background-color: #d4d4d4;
    text-align: center;
    font-size: 16px;
    color: rgba(101, 101, 101, 1);
    .midText {
      position: absolute;
      left: 50%;
      background-color: #ffffff;
      font-size: px2rem(14);
      padding: 0 15px;
      transform: translateX(-50%) translateY(-50%);
    }
  }
  .third_party {
    width: px2rem(170);
    height: px2rem(65);
    display: flex;
    margin: 0 auto;
    p {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-around;
      .third_icon {
        font-size: px2rem(30);
      }
      span {
        font-size: px2rem(14);
        color: #838383;
        font-weight: bolder;
      }
    }
  }
  .copyright_footer {
    width: 100%;
    height: px2rem(20);
    text-align: center;
    line-height: px2rem(20);
    font-size: px2rem(8);
    color: #c0c0c0;
    position: absolute;
    bottom: px2rem(10);
    left: px2rem(0);
  }
}
</style>
